package com.edward.action;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.struts2.ServletActionContext;
import org.springframework.security.annotation.Secured;

import com.edward.JsonDateProcessor;
import com.edward.PagerModel;
import com.edward.model.Stat;
import com.edward.model.StatAc;
import com.edward.model.StatDb;
import com.edward.model.StatItem;
import com.edward.service.StatAcManager;
import com.edward.service.StatDbManager;
import com.edward.service.StatItemManager;
import com.edward.service.StatManager;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class StatAction extends ActionSupport{


	private static final long serialVersionUID = 4912812398720905688L;

	private StatItemManager statItemManager;
	
	private StatManager statManager;
	
	private StatDbManager statDbManager;
	
	private StatAcManager statAcManager;
	
	private Stat stat;
	
	private StatItem statItem;
	
	private StatDb statDb;
	
	private StatAc statAc;
	
	private String type = "";
	
	private boolean flag = false;
	
	private int limit;
	
	private int start;
	
	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	
	public Stat getStat() {
		return stat;
	}

	public void setStat(Stat stat) {
		this.stat = stat;
	}

	public StatItem getStatItem() {
		return statItem;
	}

	public void setStatItem(StatItem statItem) {
		this.statItem = statItem;
	}

	public void setStatItemManager(StatItemManager statItemManager) {
		this.statItemManager = statItemManager;
	}
	
	public void setStatManager(StatManager statManager) {
		this.statManager = statManager;
	}
	
	public StatDb getStatDb() {
		return statDb;
	}

	public void setStatDb(StatDb statDb) {
		this.statDb = statDb;
	}
	
	public StatAc getStatAc() {
		return statAc;
	}

	public void setStatAc(StatAc statAc) {
		this.statAc = statAc;
	}

	public void setStatAcManager(StatAcManager statAcManager) {
		this.statAcManager = statAcManager;
	}

	public void setStatDbManager(StatDbManager statDbManager) {
		this.statDbManager = statDbManager;
	}

	@Secured("ROLE_FINANCE")
	public String add() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		statManager.addStat(stat);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	
	@Secured("ROLE_FINANCE")
		public String update() throws Exception{
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
			Stat s = statManager.getStat(stat.getId());
			stat.setUsed(s.getUsed());
			statManager.updateStat(stat);
			response.getWriter().print("{success : true , msg : '修改成功!'}");
			return null;
		}
		
	@Secured("ROLE_FINANCE")
		public String delete() throws Exception{
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
		    if(statManager.judge(stat.getId())){
		    	statManager.deleteStat(stat.getId());
		    	response.getWriter().print("{success : true , msg : '删除成功!'}");
		    }else{
		    	response.getWriter().print("{success : false , msg : '存在明细，请先删除所有明细!'}");
		    }
		    
			
			return null;
		}

	@Secured("ROLE_FINANCE")
		public String getList() throws Exception {

			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
		    JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
			cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
			PagerModel pager;
			pager =  statManager.pagerList(start, limit );
			JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
			String jsonStr = jsonObject.toString();
			response.getWriter().print(jsonStr);
			return null;
		}

	
//添加statList 的 货物明细
	@Secured("ROLE_FINANCE")
	public String addItem() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Stat s = statManager.getStat(stat.getId());
		s.setUsed(s.getUsed() + statItem.getRmoney());
		statManager.updateStat(s);
		statItem.setStat(s);
		statItemManager.addStatItem(statItem);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String updateItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Stat s = statManager.getStat(stat.getId());
        StatItem si = statItemManager.getStatItem(statItem.getId());
        s.setUsed(s.getUsed() + statItem.getRmoney() - si.getRmoney());
		statManager.updateStat(s);
		statItem.setStat(s);
		statItemManager.updateStatItem(statItem);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String deleteItem() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Stat s = statManager.getStat(stat.getId());
		StatItem sItem = statItemManager.getStatItem(statItem.getId());
		s.setUsed(s.getUsed() - sItem.getRmoney());
		statManager.updateStat(s);
		statItemManager.deleteStatItem(statItem.getId());
		response.getWriter().print("{success : true , msg : '删除成功'}");
		return null;
	}

//
	//获取grid 显示 的 分页信息
	@Secured("ROLE_FINANCE")
	public String getItems() throws Exception {
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = statItemManager.pagerStatItem(start, limit,stat.getId());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String addDb() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Stat s = statManager.getStat(stat.getId());
		s.setUsed(s.getUsed() + statDb.getMoney());
		statManager.updateStat(s);
		statDb.setStat(s);
		statDbManager.addStatDb(statDb);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String updateDb() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Stat s = statManager.getStat(stat.getId());
        StatDb si = statDbManager.getStatDb(statDb.getId());
        s.setUsed(s.getUsed() + statDb.getMoney() - si.getMoney());
		statManager.updateStat(s);
		statDb.setStat(s);
		statDbManager.updateStatDb(statDb);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String deleteDb() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Stat s = statManager.getStat(stat.getId());
		StatDb sDb = statDbManager.getStatDb(statDb.getId());
		s.setUsed(s.getUsed() - sDb.getMoney());
		statManager.updateStat(s);
		statDbManager.deleteStatDb(statDb.getId());
		response.getWriter().print("{success : true , msg : '删除成功'}");
		return null;
	}

//
	//获取grid 显示 的 分页信息
	@Secured("ROLE_FINANCE")
	public String getDbs() throws Exception {
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = statDbManager.pagerStatDb(start, limit,stat.getId());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String addAc() throws Exception{
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Stat s = statManager.getStat(stat.getId());
		s.setUsed(s.getUsed() + statAc.getMoney());
		statManager.updateStat(s);
		statAc.setStat(s);
		statAcManager.addStatAc(statAc);
		response.getWriter().print("{success : true , msg : '添加成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String updateAc() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Stat s = statManager.getStat(stat.getId());
        StatAc si = statAcManager.getStatAc(statAc.getId());
        s.setUsed(s.getUsed() + statAc.getMoney() - si.getMoney());
		statManager.updateStat(s);
		statAc.setStat(s);
		statAcManager.updateStatAc(statAc);
		response.getWriter().print("{success : true , msg : '修改成功!'}");
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	public String deleteAc() throws Exception {

		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		Stat s = statManager.getStat(stat.getId());
		StatAc sAc = statAcManager.getStatAc(statAc.getId());
		s.setUsed(s.getUsed() - sAc.getMoney());
		statManager.updateStat(s);
		statAcManager.deleteStatAc(statAc.getId());
		response.getWriter().print("{success : true , msg : '删除成功'}");
		return null;
	}

//
	//获取grid 显示 的 分页信息
	@Secured("ROLE_FINANCE")
	public String getAcs() throws Exception {
		ActionContext ctx = ActionContext.getContext();        
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
		response.setContentType("text/html;charset=UTF-8");
		JsonConfig cfg = new JsonConfig();
		cfg.registerJsonValueProcessor(java.util.Date.class, new JsonDateProcessor());
		cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonDateProcessor());
		PagerModel pager;
	    pager = statAcManager.pagerStatAc(start, limit,stat.getId());
		JSONObject jsonObject = JSONObject.fromObject(pager,cfg);
		String jsonStr = jsonObject.toString();
		response.getWriter().print(jsonStr);
		return null;
	}
	
	@Secured("ROLE_FINANCE")
	 public String getListJudge() throws Exception {
			ActionContext ctx = ActionContext.getContext();        
			HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);    
			response.setContentType("text/html;charset=UTF-8");
				response.getWriter().print("{success : true , msg : '成功!',deny:false}");
		 return null;
	 }
	
	

}
